package com.fingerchar.api.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fingerchar.core.base.service.IBaseService;
import com.fingerchar.db.domain.FcUser;
import com.fingerchar.db.domain.TtStatisticsAmount;
import com.fingerchar.db.domain.TtUsdtRecharge;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;

/**
 * 个人、团队业绩
 * @author tangzf
 * @date 2022/9/15
 */
@Service
public class TtStatisticsAmountService {

    @Autowired
    IBaseService baseService;



    public TtStatisticsAmount getByAddress(String address){

        QueryWrapper<TtStatisticsAmount> wrapper = new QueryWrapper<>();
        wrapper.eq("address", address);
        return baseService.getByCondition(TtStatisticsAmount.class, wrapper);
    }

    public int initData(String address){
        TtStatisticsAmount ttStatisticsAmount = TtStatisticsAmount.builder()
                .personalAmount(BigInteger.ZERO)
                .teamAmount(BigInteger.ZERO)
                .address(address)
                .build();
        return baseService.save(ttStatisticsAmount);
    }


    public int addPersonalAmount(String address,  BigInteger amount){
        TtStatisticsAmount statisticsAmount = getByAddress(address);
        if(null == statisticsAmount){
            initData(address);
            statisticsAmount = getByAddress(address);
        }

        TtStatisticsAmount statisticsAmount1 = new TtStatisticsAmount();
        statisticsAmount1.setId(statisticsAmount.getId());
        statisticsAmount1.setPersonalAmount(statisticsAmount.getPersonalAmount().add(amount));
        return baseService.update(statisticsAmount1);
    }

    public int addTeamAmount(String address,  BigInteger amount){
        TtStatisticsAmount statisticsAmount = getByAddress(address);
        if(null == statisticsAmount){
            initData(address);
            statisticsAmount = getByAddress(address);
        }
        TtStatisticsAmount statisticsAmount1 = new TtStatisticsAmount();
        statisticsAmount1.setId(statisticsAmount.getId());
        statisticsAmount1.setTeamAmount(statisticsAmount.getTeamAmount().add(amount));
        return baseService.update(statisticsAmount1);
    }

}
